import { Mustache } from './mustache/mustache.js'


const renderList = [
  "NAME:{{name}}",
  "EMPTY:{{nothing}}",
  "{{focus}}",
  "{{{focus}}}",
  "{{&focus}}",
  "SEX:{{msg.sex}};AGE:{{msg.age}}",
  "{{#msg}}SEX:{{sex}};AGE:{{age}};HOBBY:{{hobby}}{{/msg}}",
  "{{#subject}}{{.}}{{/subject}}",
  "EMPTY-ARR:{{#moreInfo}}empty-arr{{/moreInfo}}\nARRAY: {{#subject}}arr {{/subject}}\n",
  "EMPTY-ARR:{{^moreInfo}}empty-arr{{/moreInfo}}\nARRAY: {{^subject}}arr {{/subject}}\n",
  "{{!name}}.",
  "NAME:{{name}}\nINFO:\n{{>template}}",
  "NAME:{{name}}\nNAME:<%name%>\n",
  "NAME:{{name}}\nNAME:<%name%>\n"
]

const data = {
  name: "cai",
  msg: {
    sex: "male",
    age: "66",
    hobby: "reading"
  },
  focus: "<span>sleep<span>",
  subject: [" Ch ", " En ", " Math ", " physics "],
  moreInfo: []
}

const testList = [
  { name: "解析", description: "mustache会解析出‘{{}}’内包含的内容" },
  { name: "空值", description: "未找到对应值则不显示" },
  { name: "HTML转义", description: "'{{}}'默认HTML转义" },
  { name: "禁用HTML转义", description: "'{{{}}}'可以禁用HTML转义" },
  { name: "取消转义", description: "'&'可以取消转义变量" },
  { name: "对象属性", description: "'.'可用于访问对象属性的键" },
  { name: "构建块", description: "{{#}}开始一个部分,同时{{/}}结束它.两个标签之间的文本的行为由键的值决定." },
  { name: "枚举", description: "{{.}}表示枚举，可以循环输出整个字符串数组" },
  { name: "if", description: "{{#}}{{/}}不为空数组时，内部才会展示内容" },
  { name: "else", description: "{{^}}{{/}}为空数组时，内部会展示内容" },
  { name: "注释", description: "!表示注释,注释后不会渲染输出任何内容" },
  { name: "模块", description: "以{{>}}开始表示子模块,可以将复杂的结构拆分成几个小的子模块" },
  { name: "自定义标签", description: "标签可以自定义" },
  { name: "预编译", description: "" }
]


function mustacheTest_Render_1() {
  let result = 'NAME:cai'
  let renderStr = ''
  let startTime = Date.now();
  for (let i = 0; i < 100000; i++) {
    renderStr = Mustache.render(renderList[0], data)
  }
  let endTime = Date.now();
  if (renderStr != result) {
    throw 'Mustache.render error'
  }
  // console.log(`Jmustache_render_001: ${endTime - startTime} ms`)
  print(`Jmustache_render_001: ${endTime - startTime} ms`)
}


function mustacheTest_Render_2() {
  let result = 'EMPTY:'
  let renderStr = ''
  let startTime = Date.now();
  for (let i = 0; i < 100000; i++) {
    renderStr = Mustache.render(renderList[1], data)
  }
  let endTime = Date.now();
  if (renderStr != result) {
    throw 'Mustache.render error'
  }
  // console.log(`Jmustache_render_002: ${endTime - startTime} ms`)
  print(`Jmustache_render_002: ${endTime - startTime} ms`)
}


function mustacheTest_Render_3() {
  let result = '&lt;span&gt;sleep&lt;span&gt;'
  let renderStr = ''
  let startTime = Date.now();
  for (let i = 0; i < 100000; i++) {
    renderStr = Mustache.render(renderList[2], data)
  }
  let endTime = Date.now();
  if (renderStr != result) {
    throw 'Mustache.render error'
  }
  // console.log(`Jmustache_render_003: ${endTime - startTime} ms`)
  print(`Jmustache_render_003: ${endTime - startTime} ms`)
}


function mustacheTest_Render_4() {
  let result = '<span>sleep<span>'
  let renderStr = ''
  let startTime = Date.now();
  for (let i = 0; i < 100000; i++) {
    renderStr = Mustache.render(renderList[3], data)
  }
  let endTime = Date.now();
  if (renderStr != result) {
    throw 'Mustache.render error'
  }
  // console.log(`Jmustache_render_004: ${endTime - startTime} ms`)
  print(`Jmustache_render_004: ${endTime - startTime} ms`)
}


function mustacheTest_Render_5() {
  let result = '<span>sleep<span>'
  let renderStr = ''
  let startTime = Date.now();
  for (let i = 0; i < 100000; i++) {
    renderStr = Mustache.render(renderList[4], data)
  }
  let endTime = Date.now();
  if (renderStr != result) {
    throw 'Mustache.render error'
  }
  // console.log(`Jmustache_render_005: ${endTime - startTime} ms`)
  print(`Jmustache_render_005: ${endTime - startTime} ms`)
}


function mustacheTest_Render_6() {
  let result = 'SEX:male;AGE:66'
  let renderStr = ''
  let startTime = Date.now();
  for (let i = 0; i < 100000; i++) {
    renderStr = Mustache.render(renderList[5], data)
  }
  let endTime = Date.now();
  if (renderStr != result) {
    throw 'Mustache.render error'
  }
  // console.log(`Jmustache_render_006: ${endTime - startTime} ms`)
  print(`Jmustache_render_006: ${endTime - startTime} ms`)
}


function mustacheTest_Render_7() {
  let result = 'SEX:male;AGE:66;HOBBY:reading'
  let renderStr = ''
  let startTime = Date.now();
  for (let i = 0; i < 100000; i++) {
    renderStr = Mustache.render(renderList[6], data)
  }
  let endTime = Date.now();
  if (renderStr != result) {
    throw 'Mustache.render error'
  }
  // console.log(`Jmustache_render_007: ${endTime - startTime} ms`)
  print(`Jmustache_render_007: ${endTime - startTime} ms`)
}


function mustacheTest_Render_8() {
  let result = ' Ch  En  Math  physics '
  let renderStr = ''
  let startTime = Date.now();
  for (let i = 0; i < 100000; i++) {
    renderStr = Mustache.render(renderList[7], data)
  }
  let endTime = Date.now();
  if (renderStr != result) {
    throw 'Mustache.render error'
  }
  // console.log(`Jmustache_render_008: ${endTime - startTime} ms`)
  print(`Jmustache_render_008: ${endTime - startTime} ms`)
}


function mustacheTest_Render_9() {
  let result = `EMPTY-ARR:
ARRAY: arr arr arr arr 
`
  let renderStr = ''
  let startTime = Date.now();
  for (let i = 0; i < 100000; i++) {
    renderStr = Mustache.render(renderList[8], data)
  }
  let endTime = Date.now();
  if (renderStr != result) {
    throw 'Mustache.render error'
  }
  // console.log(`Jmustache_render_009: ${endTime - startTime} ms`)
  print(`Jmustache_render_009: ${endTime - startTime} ms`)
}


function mustacheTest_Render_10() {
  let result = `EMPTY-ARR:empty-arr
ARRAY: 
`
  let renderStr = ''
  let startTime = Date.now();
  for (let i = 0; i < 100000; i++) {
    renderStr = Mustache.render(renderList[9], data)
  }
  let endTime = Date.now();
  if (renderStr != result) {
    throw 'Mustache.render error'
  }
  // console.log(`Jmustache_render_010: ${endTime - startTime} ms`)
  print(`Jmustache_render_010: ${endTime - startTime} ms`)
}


function mustacheTest_Render_11() {
  let result = '.'
  let renderStr = ''
  let startTime = Date.now();
  for (let i = 0; i < 100000; i++) {
    renderStr = Mustache.render(renderList[10], data)
  }
  let endTime = Date.now();
  if (renderStr != result) {
    throw 'Mustache.render error'
  }
  // console.log(`Jmustache_render_011: ${endTime - startTime} ms`)
  print(`Jmustache_render_011: ${endTime - startTime} ms`)
}


function mustacheTest_Render_12() {
  let result = `NAME:cai
INFO:
SEX:male;AGE:66;HOBBY:reading`
  let partials = { template: "{{#msg}}SEX:{{sex}};AGE:{{age}};HOBBY:{{hobby}}{{/msg}}" };
  let renderStr = ''
  let startTime = Date.now();
  for (let i = 0; i < 100000; i++) {
    renderStr = Mustache.render(renderList[11], data, partials)
  }
  let endTime = Date.now();
  if (renderStr != result) {
    throw 'Mustache.render error'
  }
  // console.log(`Jmustache_render_012: ${endTime - startTime} ms`)
  print(`Jmustache_render_012: ${endTime - startTime} ms`)
}


function mustacheTest_Render_13() {
  let result = `NAME:{{name}}
NAME:cai
`
  let customTags = ['<%', '%>'];
  let obj = {};
  let renderStr = ''
  let startTime = Date.now();
  for (let i = 0; i < 100000; i++) {
    renderStr = Mustache.render(renderList[12], data, obj, customTags)
  }
  let endTime = Date.now();
  if (renderStr != result) {
    throw 'Mustache.render error'
  }
  // console.log(`Jmustache_render_013: ${endTime - startTime} ms`)
  print(`Jmustache_render_013: ${endTime - startTime} ms`)
}


function mustacheTest_Parse_14() {
  let result = `NAME:cai
NAME:<%name%>
`
  let renderStr = ''
  let startTime = Date.now();
  for (let i = 0; i < 100000; i++) {
    Mustache.parse(renderList[13])
  }
  let endTime = Date.now();
  renderStr = Mustache.render(renderList[13], data)
  if (renderStr != result) {
    throw 'Mustache.render error'
  }
  // console.log(`Jmustache_parse_014: ${endTime - startTime} ms`)
  print(`Jmustache_parse_014: ${endTime - startTime} ms`)
}


function mustacheTest_ClearCache_15() {
  let startTime = Date.now();
  for (let i = 0; i < 100000; i++) {
    Mustache.clearCache()
  }
  let endTime = Date.now();
  // console.log(`Jmustache_clearCache_015: ${endTime - startTime} ms`)
  print(`Jmustache_clearCache_015: ${endTime - startTime} ms`)
}

function mustacheTest() {
  mustacheTest_Render_1();
  mustacheTest_Render_2();
  mustacheTest_Render_3();
  mustacheTest_Render_4();
  mustacheTest_Render_5();
  mustacheTest_Render_6();
  mustacheTest_Render_7();
  mustacheTest_Render_8();
  mustacheTest_Render_9();
  mustacheTest_Render_10();
  mustacheTest_Render_11();
  mustacheTest_Render_12();
  mustacheTest_Render_13();
  mustacheTest_Parse_14();
  mustacheTest_ClearCache_15();
}

mustacheTest();